package com.lagou.homework18.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;

/**
 * @author mcl
 * @date 2021/10/25 18:14
 * @description
 */
@RestController
@RequestMapping("/jd")
@Slf4j
public class JdController {

    @Autowired
    private RestTemplate restTemplate;
    @Value("${log.url}")
    private String logUrl;

    @GetMapping("/{id}")
    public String get(@RequestParam String userId, @PathVariable String id) {
        log.info("[用户:{}]查看[简历:{}]",userId,id);

        HttpEntity<MultiValueMap<String, String>> r = getHttpEntity("getJd", id, userId);
        restTemplate.postForEntity(logUrl,r,String.class);
        return "JdInfo:"+id;
    }

    @PutMapping("/{id}/collect")
    public String collect(@RequestParam String userId, @PathVariable String id) {
        log.info("[用户:{}]收藏[简历:{}]",userId,id);
        HttpEntity<MultiValueMap<String, String>> r = getHttpEntity("collectJd", id, userId);
        restTemplate.postForEntity(logUrl,r,String.class);
        return "CollectJd:"+id;
    }

    @PutMapping("/{id}/send")
    public String send(@RequestParam String userId, @PathVariable String id) {
        log.info("[用户:{}]投递[简历:{}]",userId,id);
        HttpEntity<MultiValueMap<String, String>> r = getHttpEntity("sendJd", id, userId);
        restTemplate.postForEntity(logUrl,r,String.class);
        return "SendJd:"+id;
    }

    @PostMapping("/{id}/upload")
    public String upload(@RequestParam String userId, @PathVariable String id) {
        log.info("[用户:{}]上传[简历:{}]",userId,id);
        HttpEntity<MultiValueMap<String, String>> r = getHttpEntity("uploadJd", id, userId);
        restTemplate.postForEntity(logUrl,r,String.class);
        return "UploadJd:"+id;
    }

    private HttpEntity<MultiValueMap<String, String>> getHttpEntity(String action, String jdId, String userId) {
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("action",action);
        params.add("jdId",jdId);
        params.add("userId",userId);
        params.add("time", String.valueOf(System.currentTimeMillis()));
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/x-www-form-urlencoded");
        return new HttpEntity<>(params, headers);
    }
}
